package com.example.bootasync.listener;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONParser;
import com.example.bootasync.service.IUserService;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author : JinMing Zhou
 * @description: 接受消息
 * @date : 2023/6/1 10:53
 */
@Component
public class IAsyncListener {

    @Resource
    private IUserService userService;

    /**
     * 监听来自生产者的消息
     * @param msg 消息内容
     */
    @RabbitListener(queuesToDeclare = @Queue("async_queue"))
    public void getProdMsg(String msg){
        JSONObject jsonObject = new JSONObject(msg);
        Integer userId = (Integer) jsonObject.get("userId");
        Integer source = (Integer) jsonObject.get("source");
        userService.addSource(Long.valueOf(userId), source);
    }
}
